package com.itheima.tlias.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

public class DegreeConverter implements Converter<Integer> {

    @Override
    public Class<?> supportJavaTypeKey() {
        // 支持的 Java 类型是 Integer
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        // 支持的 Excel 类型是字符串
        return CellDataTypeEnum.STRING;
    }

    /**
     * 读取 Excel 数据时调用
     */
    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) {
        // 获取单元格中的字符串值
        String degree = context.getReadCellData().getStringValue();
//1: 初中, 2: 高中, 3: 大专, 4: 本科, 5: 硕士, 6: 博士
        if ("初中".equals(degree)){
            return 1;
        }else if ("高中".equals(degree)) {
            return 2;
        } else if ("大专".equals(degree)) {
            return 3;
        } else if ("本科".equals(degree)) {
            return 4;
        } else if ("硕士".equals(degree)) {
            return 5;
        } else if ("博士".equals(degree)) {
            return 6;
        }
        return null;
    }

    /**
     * 写入 Excel 数据时调用
     */
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        // 获取 Java 对象中的值
        Integer isCollege = context.getValue();
        if (isCollege == 1) {
            return new WriteCellData<>("初中"); // 1 转换为 "初中"
        } else if (isCollege == 2) {
            return new WriteCellData<>("高中"); // 0 转换为 "高中"
        } else if (isCollege == 3) {
            return new WriteCellData<>("大专"); // 0 转换为 "大专"
        } else if (isCollege == 4) {
            return new WriteCellData<>("本科"); // 0 转换为 "本科"
        }else if (isCollege ==  5) {
            return new WriteCellData<>("硕士"); // 0 转换为 "硕士"
        }else if (isCollege == 6) {
            return new WriteCellData<>("博士"); // 0 转换为 "博士"
        }
        return new WriteCellData<>("未知");
    }
}